import React from 'react';
import { Col, Icon } from 'antd';

import _ from 'lodash';
import { connect } from 'dva';
import { TextWithTags } from '@/components/BaseTable';

@connect(state => ({}))
export default class IdCellRenderer extends React.PureComponent {
  state = {};

  addParking = () => {
    let rowData = _.cloneDeep(this.props.rowData);
    delete rowData.children;
    delete rowData.supplyDate;
    rowData = {
      code: null,
      num: 0,
      num_parking: 0,
      num_parking_forsale: 0,
      averagePrice_parking: rowData.averagePrice_parking,
      id: `${+new Date()}${rowData.id}`,
      name: null,
      pid: rowData.id,
      isParking: 1,
      // isSplit: 1,
      saleArea: null,
      type: 'org_salingparking',
      isAdd: true,
    };
    const { dispatch } = this.props;
    dispatch({
      type: 'PriceManageItems/saveRows',
      payload: { rowData, category: this.props.category, isMarketing: this.props.isMarketing },
    });
  };

  addHousePackage = () => {
    let rowData = _.cloneDeep(this.props.rowData);
    delete rowData.children;
    delete rowData.supplyDate;
    console.log(rowData, 'rowData');
    rowData = {
      code: null,
      num: rowData.left_num_house > 0 ? rowData.left_num_house : 1,
      num_house: rowData.left_num_house > 0 ? rowData.left_num_house : 1,
      averagePrice_house: rowData.averagePrice_house,
      areaPart: '请选择面积段',
      isSupply: rowData.isSupply,
      id: `${+new Date()}${rowData.id}`,
      name: null,
      pid: rowData.id,
      saleArea: rowData.left_saleArea > 0 ? rowData.left_saleArea : 0,
      type: 'org_house_package',
      isAdd: true,
    };
    const { dispatch } = this.props;
    dispatch({
      type: 'PriceManageItems/saveRows',
      payload: { rowData, category: this.props.category, isMarketing: this.props.isMarketing },
    });
  };

  removeRow = () => {
    let rowData = _.cloneDeep(this.props.rowData);
    rowData = {
      ...rowData,
      isNew: false,
      isRemove: true,
    };
    const { dispatch } = this.props;
    dispatch({
      type: 'PriceManageItems/saveRows',
      payload: { rowData, category: this.props.category, isMarketing: this.props.isMarketing },
    });
  };

  render() {
    const {
      editing,
      rowData,
      cellData,
      batchAdjust,
      showCopyPriceComponent,
      showCopyHouseComponent,
    } = this.props;

    if (rowData.type === 'org_house_package') {
      cellData.tags = [];
    }
    return (
      <>
        <>
          <TextWithTags {...this.props} />
        </>
        {/* 普调版本不能复制 */}
        {editing && batchAdjust * 1 === 0 ? (
          <>
            <Col>
              {this.props.category === 'investment' &&
              rowData.type === 'org_product' &&
              (!rowData.businessTypeName ||
                rowData.businessTypeName === '可售' ||
                rowData.businessTypeName === '阶段性自持') &&
              rowData.isParking !== 1 &&
              rowData.errorAreaSegment === false &&
              rowData.errorNum === false &&
              rowData.errorPrice === false &&
              rowData.errorSaleArea === false &&
              rowData.errorSplitNum === false &&
              rowData.errorSplitSaleArea === false ? (
                <Icon
                  onClick={() =>
                    // 复制均价
                    showCopyPriceComponent(rowData)
                  }
                  style={{ fontSize: '16px', color: '#08c' }}
                  type="copy"
                />
              ) : null}
              {rowData.isHouseParent &&
              (!rowData.businessTypeName ||
                rowData.businessTypeName === '可售' ||
                rowData.businessTypeName === '阶段性自持') &&
              rowData.errorAreaSegment === false &&
              rowData.errorNum === false &&
              rowData.errorPrice === false &&
              rowData.errorSaleArea === false &&
              rowData.errorSplitNum === false &&
              rowData.errorSplitSaleArea === false ? (
                <Icon
                  onClick={() =>
                    // 复制面积段
                    showCopyHouseComponent(rowData)
                  }
                  style={{ fontSize: '16px', color: '#08c' }}
                  type="copy"
                />
              ) : null}
              {rowData.canSplit === true && rowData.isHouseParent ? (
                <>
                  <Icon
                    style={{ fontSize: '16px', color: '#08c' }}
                    type="plus-circle"
                    theme="filled"
                    onClick={this.addHousePackage}
                  />
                </>
              ) : null}
              {rowData.canSplit === true && rowData.isParkingParent ? (
                <>
                  <Icon
                    style={{ fontSize: '16px', color: '#08c' }}
                    type="plus-circle"
                    theme="filled"
                    onClick={this.addParking}
                  />
                </>
              ) : null}
              {rowData.canRemove ? (
                <Icon
                  onClick={this.removeRow}
                  style={{ fontSize: '16px', color: '#08c' }}
                  type="minus-circle"
                  theme="filled"
                />
              ) : null}
            </Col>
          </>
        ) : null}
      </>
    );
  }
}
